<?php
/**
 * BaseController
 *
 * @author
 * @version
 */
require_once 'Zend/Controller/Action.php';
require_once 'configs/Config.php';
require_once 'configs/PerspectiveConfig.php';
require_once 'models/User.php';
require_once 'models/Permission.php';
require_once 'HtmlPurifier/HTMLPurifier.auto.php';
class BaseController extends Zend_Controller_Action {
	
	protected $footerArray;
	
	protected $footer;
	
	protected $headerArray;
	
	protected $header;
	
	protected $leftSideArray;
	
	protected $leftSide;
	
	protected $rightSideArray;
	
	protected $rightSide;
	
	protected $mainMenuArray;
	
	protected $mainMenu;
	
	protected $userUpperArray;
	
	protected $userUpper;
	
	protected $rootDir;
	
	protected $mainCopyBk;
	
	public function getPurifyWhiteList()	{
		//default: no white list
		return array();
	}
	
	/**
	 * 
	 * Purify user input, using HTMLPurifier
	 */
	public function purify() {
		$purifier = new HTMLPurifier ();
		foreach ( $this->getRequest ()
			->getParams () as $param => $value ) {
			if (in_array($param, $this->getPurifyWhiteList()))	{
				//dont purify if the param is in the whitelist
				continue;
			}
			$this->_setParam ( $param, $this->purifyElement ( $purifier, $value ) );
		}
	}
	
	protected function purifyElement($purifier, $value) {
		if (is_object ( $value )) {
			return $value;
		}
		if (is_array ( $value )) {
			foreach ( $value as $key => $v ) {
				$value [$key] = $this->purifyElement ( $purifier, $v );
			}
		} else {
			$value = $purifier->purify ( $value );
		}
		return $value;
	}
	
	// this function is called each time a new action is called
	// so every permission checking should be placed in here
	// or in init() ?
	public function init() {
		$this->initVars ();
		$this->purify ();
	}
	
	public function getFooterArray() {
		// NOTES: Override this method in subclasses to change the default footer
		return ViewConfig::$defFooter;
	}
	
	public function getHeaderArray() {
		// NOTES: Override this method in subclasses to change the default header
		return ViewConfig::$defHeader;
	}
	
	public function getLeftSideArray() {
		// NOTES: Override this method in subclasses to change the default left side bar
		return ViewConfig::$defLeftSide;
	}
	
	public function getRightSideArray() {
		// NOTES: Override this method in subclasses to change the default right side bar
		return ViewConfig::$defRightSide;
	}
	
	public function getMainMenuArray() {
		// NOTES: Override this method in subclasses to change the default right side bar
		return ViewConfig::$defMainMenu;
	}
	
	public function getUserUpperArray() {
		return ViewConfig::$userUpper;
	}
	
	public function getFooter() {
		$s = "";
		foreach ( $this->footerArray as $string )
			$s = $s . $this->view
				->render ( ViewConfig::$includeFolder . '/' . $string );
		return $s;
	}
	
	public function getHeader() {
		$s = "";
		foreach ( $this->headerArray as $string ) {
			$s = $s . $this->view
				->render ( ViewConfig::$includeFolder . '/' . $string );
		}
		return $s;
	}
	
	public function getLeftSideBar() {
		$s = "";
		foreach ( $this->leftSideArray as $string )
			$s = $s . $this->view
				->render ( ViewConfig::$includeFolder . '/' . $string );
		return $s;
	}
	
	public function getRightSideBar() {
		$s = "";
		foreach ( $this->rightSideArray as $string )
			$s = $s . $this->view
				->render ( ViewConfig::$includeFolder . '/' . $string );
		return $s;
	}
	
	public function getMainMenu() {
		$s = "";
		foreach ( $this->mainMenuArray as $string )
			$s = $s . $this->view
				->render ( ViewConfig::$includeFolder . '/' . $string );
		return $s;
	}
	
	public function getUserUpper() {
		$s = "";
		foreach ( $this->userUpperArray as $string )
			$s = $s . $this->view
				->render ( ViewConfig::$includeFolder . '/' . $string );
		return $s;
	}
	
	public function onUpdateVars() {
		$this->footer = $this->getFooter ();
		$this->header = $this->getHeader ();
		$this->leftSide = $this->getLeftSideBar ();
		$this->rightSide = $this->getRightSideBar ();
		$this->mainMenu = $this->getMainMenu ();
		$this->userUpper = $this->getUserUpper ();
	}
	
	public function resetVars() {
		$this->footerArray = array ();
		$this->headerArray = array ();
		$this->leftSideArray = array ();
		$this->rightSideArray = array ();
		$this->mainMenuArray = array ();
		$this->userUpperArray = array ();
	}
	
	public function initVars() {
		$this->view->referer = $this->getLastPage ();
		$this->mainCopyBk = "background-color: white;";
		$this->view->logoDivClass = "doNotDisplay";
		$this->view->root = DirConfig::$root;
		$this->view->sitename = DirConfig::$sitename;
		$this->rootDir = DirConfig::$root;
		$this->view->pageTitle = GlobalConfig::$pageTitle;
		if ($this->_hasParam ( 'display' )) {
			$display = $this->getRequest ()
				->getParam ( 'display' );
			if ($display == 'nolayout') {
				$this->view
					->layout ()
					->disableLayout ();
			}
		}
		$this->resetVars ();
		if (! $this->_hasParam ( 'content-only' )) {
			if (! $this->_hasParam ( 'no-footer' )) {
				$this->footerArray = $this->getFooterArray ();
			}
			if (! $this->_hasParam ( 'no-header' )) {
				$this->headerArray = $this->getHeaderArray ();
			}
			if (! $this->_hasParam ( 'no-side-bar' )) {
				$this->leftSideArray = $this->getLeftSideArray ();
			}
			if (! $this->_hasParam ( 'no-right-bar' )) {
				$this->rightSideArray = $this->getRightSideArray ();
			}
			if (! $this->_hasParam ( 'no-menu' )) {
				$this->mainMenuArray = $this->getMainMenuArray ();
			}
			if (! $this->_hasParam ( 'no-user-area' )) {
				$this->userUpperArray = $this->getUserUpperArray ();
			}
		}
		if ($this->_hasParam ( 'script-disabled' )) {
			$this->view->disableScript = true;
		}
	}
	
	public function renderLayout() {
		$this->layoutRendered = true;
		$this->onUpdateVars ();
		$layout = $this->view
			->layout ();
		$layout->mainCopyBk = $this->mainCopyBk;
		$layout->header = $this->header;
		$layout->footer = $this->footer;
		$layout->leftbar = $this->leftSide;
		$layout->rightbar = $this->rightSide;
		$layout->mainmenu = $this->mainMenu;
		$layout->userUpper = $this->userUpper;
	}
	
	/**
	 * Render the layout
	 * (non-PHPdoc)
	 * @see library/Zend/Controller/Zend_Controller_Action::postDispatch()
	 */
	public function postDispatch() {
		$this->renderLayout ();
		// FIXME : should I place it here? a simple action to display a message wherever a page is loaded
		if($this->_hasParam("msg")){
			$msg = $this->_getParam("msg");
			$str = "<script language='javascript'>";
			$str .= "$(document).ready(function(){"."$.facebox('".$msg."')});";
			$str .= "</script>";
			echo $str;
		}
		// end
	}
	
	/**
	 * 
	 * An alternative to the built-in _redirect() method
	 * which supports controller-action based url
	 * @param $action
	 * @param $controller
	 * @param $module
	 * @param $params
	 */
	public function redirect($action, $controller = null, $module = null, array $params = array()) {
		$redirector = $this->_helper
			->getHelper ( 'redirector' );
		//		$redirector->setUseAbsoluteUri ();
		$request = $this->getRequest ();
		$params ['refC'] = $request->getControllerName ();
		$params ['refA'] = $request->getActionName ();
		return $redirector->gotoSimple ( $action, $controller, $module, $params );
	}
	
	/**
	 * 
	 * Convenient function for redirecting to homepage
	 */
	public function returnToHomepage() {
		$this->redirect ( 'index', 'index' );
	}
	
	public function getLastPage() {
		if ($this->_hasParam ( 'refC' )) {
			$controller = $this->_getParam ( 'refC' );
		} else {
			$controller = 'index';
		}
		if ($this->_hasParam ( 'refA' )) {
			$action = $this->_getParam ( 'refA' );
		} else {
			$action = 'index';
		}
		return array ('controller' => $controller, 'action' => $action );
	}
	
	public function returnToLastPage() {
		$referer = $this->getLastPage ();
		$controller = $referer ['controller'];
		$action = $referer ['action'];
		$this->redirect ( $controller, $action );
	}
	
	public function getRootDir() {
		return $this->rootDir;
	}
}
?>